package controllers

import (
	"meituan/apps/admin/models"
	"meituan/apps/admin/service/impl"

	"github.com/gin-gonic/gin"
)

var (
	service *impl.UserServiceImpl = new(impl.UserServiceImpl)
)

func Login(ctx *gin.Context) {
	user := new(models.Users)
	err := ctx.ShouldBindJSON(user)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "请填写完整表单",
		})
		return
	}
	token, e := service.Login(user)
	if e != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户名或密码错误",
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": token,
	})
}

func Register(ctx *gin.Context) {
	user := new(models.Users)

	err := ctx.ShouldBindJSON(user)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  err.Error(),
		})
		return
	}

	success := service.Register(user)
	if success != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  success.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
}
